911D - Inversion Counting - CodeForces Solution


brute force math *1800

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <map>

using namespace std;

const int N = 2010;
int a[N], b[N], cnt[N];
int ans = 0;
int pos[N];
int n, m;
void merge_sort(int l, int r)
{
    if (l >= r) return;

    int mid = l + r >> 1;
    merge_sort(l, mid), merge_sort(mid + 1, r);

    int i = l, j = mid + 1, k = 0;
    while (i <= mid && j <= r) {
        if (a[i] <= a[j]) {
            b[k++] = a[i++];
        }
        else {         
            ans += mid - i + 1;
            pos[j] += mid - i + 1;
            b[k++] = a[j++];
        }
    }
    while (i <= mid) b[k++] = a[i++];
    while (j <= r) b[k++] = a[j++];

    for (int p = 0, q = l; p < k; p++, q++) a[q] = b[p];
}
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    merge_sort(1, n);

    int res = ans & 1;
    cin >> m;
    for (int i = 1; i <= m; i++) {
        int l, r;
        cin >> l >> r;
        int len = r - l + 1;
        if (len * (len - 1) / 2 & 1) res ^= 1;    
        if (res % 2 != 0) cout << "odd" << endl;
        else cout << "even" << endl;
    }

    system("pause");
    return 0;
}
    			 	 		 		      		     			


Comments

Submit
0 Comments
More Questions

765A - Neverending competitions
1303A - Erasing Zeroes
1005B - Delete from the Left
94A - Restoring Password
1529B - Sifid and Strange Subsequences
1455C - Ping-pong
1644C - Increase Subarray Sums
1433A - Boring Apartments
1428B - Belted Rooms
519B - A and B and Compilation Errors
1152B - Neko Performs Cat Furrier Transform
1411A - In-game Chat
119A - Epic Game
703A - Mishka and Game
1504C - Balance the Bits
988A - Diverse Team
1312B - Bogosort
1616B - Mirror in the String
1660C - Get an Even String
489B - BerSU Ball
977C - Less or Equal
1505C - Fibonacci Words
1660A - Vasya and Coins
1660E - Matrix and Shifts
1293B - JOE is on TV
1584A - Mathematical Addition
1660B - Vlad and Candies
1472C - Long Jumps
1293D - Aroma's Search
918A - Eleven